package org.agilelang.editor;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import org.agilelang.editor.listener.DBSync;
import org.agilelang.editor.listener.DBtoAgileLang;
import org.agilelang.editor.listener.FileCloseProject;
import org.agilelang.editor.listener.FileExit;
import org.agilelang.editor.listener.FileNewFile;
import org.agilelang.editor.listener.FileNewProject;
import org.agilelang.editor.listener.FileOpenProject;
import org.agilelang.editor.listener.FileSave;
import org.agilelang.editor.listener.GenerateCakePHP2;
import org.agilelang.editor.listener.GenerateStruts2;
import org.agilelang.editor.listener.ToolGenerateDoc;
import static org.agilelang.editor.tool.I18n._;
import org.agilelang.editor.tool.Util;
import org.agilelang.plugin.load.LoadPlugins;
import org.agilelang.plugin.manager.DlgManagerPlugins;

public class ViewMenu {

    ViewFrame frame;
    JMenuBar menuBar;
    //menu file
    private JMenuItem newProjectItem;
    private JMenuItem newFileItem;
    private JMenuItem openProjectItem;
    private JMenuItem closeProjectItem;
    private JMenuItem saveItem;
    private JMenuItem exitItem;
    //menu edit
    private JMenuItem cutItem;
    private JMenuItem copyItem;
    private JMenuItem pasteItem;
    private JMenuItem formatItem;
    private JMenuItem selectAllItem;
    private Action cutAction = new DefaultEditorKit.CutAction();
    private Action copyAction = new DefaultEditorKit.CopyAction();
    private Action pasteAction = new DefaultEditorKit.PasteAction();
    
    //menus
    JMenu menuGenerate = new JMenu("Generate");
    JMenu menuDB = new JMenu("Data Base");
    JMenu menuTool = new JMenu("Tool");

    ViewMenu(ViewFrame frame) {
        this.frame = frame;
        menuBar = new JMenuBar();
        initMenuFile();
        initMenuEdit();
        initMenuGenerate();
        initMenuDB();
        initMenuTool();
        initMenuHelp();
        //initMenuLoadPlugins();
    }

    public JMenuBar getMenuBar() {
        return menuBar;
    }

    ViewMenu() {
    }

    private void initMenuFile() {
        JMenu fileMenu = Util.createMenu("menu_file");
        menuBar.add(fileMenu);

        newProjectItem = Util.createMenuItem("menu_project_item_new", "applications-other.png");
        newProjectItem.addActionListener(new FileNewProject(frame));

        newFileItem = Util.createMenuItem("New File");
        newFileItem.addActionListener(new FileNewFile(frame));

        openProjectItem = Util.createMenuItem("menu_project_item_open");
        openProjectItem.addActionListener(new FileOpenProject(frame));

        closeProjectItem = Util.createMenuItem("menu_project_item_close");
        closeProjectItem.addActionListener(new FileCloseProject(frame));

        saveItem = Util.createMenuItem("menu_file_item_save", "media-floppy.png");
        saveItem.addActionListener(new FileSave(frame));

        exitItem = Util.createMenuItem("menu_file_item_exit");
        exitItem.addActionListener(new FileExit(frame));

        fileMenu.add(newProjectItem);
        fileMenu.add(newFileItem);
        fileMenu.addSeparator();
        fileMenu.add(openProjectItem);
        fileMenu.add(closeProjectItem);
        fileMenu.add(saveItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);
    }

    private void initMenuEdit() {
        JMenu editMenu = Util.createMenu("menu_edit");
        menuBar.add(editMenu);

        cutItem = Util.createMenuItem("menu_edit_item_cut");
        copyItem = Util.createMenuItem("menu_edit_item_copy");
        pasteItem = Util.createMenuItem("menu_edit_item_paste");
        formatItem = Util.createMenuItem("menu_edit_item_format");
        selectAllItem = Util.createMenuItem("menu_edit_item_selectall");

        cutItem.addActionListener(cutAction);
        copyItem.addActionListener(copyAction);
        pasteItem.addActionListener(pasteAction);
        formatItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.format();
            }
        });
        editMenu.add(cutItem);
        editMenu.add(copyItem);
        editMenu.add(pasteItem);
        editMenu.addSeparator();
        editMenu.add(formatItem);
        editMenu.addSeparator();
        editMenu.add(selectAllItem);
    }

    private void initMenuHelp() {
        JMenu helpMenu = Util.createMenu("menu_help");
        menuBar.add(helpMenu);

        JMenuItem aboutItem = Util.createMenuItem("menu_help_item_about");
        aboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, _("dialog_about_msg"),
                        _("dialog_about_title"), JOptionPane.INFORMATION_MESSAGE);
            }
        });
        helpMenu.add(aboutItem);
    }

    private void initMenuGenerate() {
        menuBar.add(menuGenerate);

        JMenuItem cakephp = Util.createMenuItem("CakePHP 2");
        cakephp.addActionListener(new GenerateCakePHP2(frame));

        JMenuItem struts = Util.createMenuItem("Java Struts2");
         struts.addActionListener(new GenerateStruts2(frame));

        menuGenerate.add(cakephp);
        menuGenerate.add(struts);

    }

    private void initMenuDB() {
        menuBar.add(menuDB);
        JMenuItem dbtoagile = Util.createMenuItem("DB to AgileLang");
        dbtoagile.addActionListener(new DBtoAgileLang(frame));
        menuDB.add(dbtoagile);

        JMenuItem dbsync = Util.createMenuItem("DB Sync");
        dbsync.addActionListener(new DBSync(frame));
        menuDB.add(dbsync);

    }

    private void initMenuTool() {
        menuBar.add(menuTool);
        JMenuItem agiletodoc = Util.createMenuItem("Generate Documentation");
        agiletodoc.addActionListener(new ToolGenerateDoc(frame));
        menuTool.add(agiletodoc);
    }

    public ActionListener plugins() {
        ActionListener action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                DlgManagerPlugins dialog = new DlgManagerPlugins(new javax.swing.JFrame(), true, "plugins/plugins.xml");
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        };
        return action;
    }

    private void initMenuLoadPlugins() {
        JMenuItem generatorConfig = Util.createMenuItem("plugins", "preferences-system.png");
        generatorConfig.addActionListener(plugins());
        menuTool.add(generatorConfig);

        LoadPlugins.load("plugins/plugins.xml", menuGenerate, menuDB, menuTool);
    }

}
